# -*- coding:utf-8 -*-
import os,sys
import re
import traceback
import time
sys.path.append(os.path.join(os.path.abspath(os.path.dirname(__file__)), os.pardir))
import supeanut_config
from CommonLib.mylog import mylog
from CommonLib.db.MongoDBTool import MongoDBTool


'''
作者：supeanut
创建时间：2016-xx-xx xx:xx:xx
功能描述：
	xxx
	xxxxx
相关配置：
	supeanut_config.XXX
历史改动：
	2016-xx-xx: xxxxxx
'''
class MongoSetup:
	# init what
	def __init__(self):
		#self.param = supeanut_config.PARAM
		self.mongo = MongoDBTool()
		self.db_name = supeanut_config.MONGO_DB
		self.coll_daydata = supeanut_config.MONGO_COLL_DAYDATA
		self.coll_stockinfo = supeanut_config.MONGO_COLL_STOCKINFO
		self.coll_trade_calendar = supeanut_config.MONGO_COLL_TRADE_CALENDAR
		self.coll_exrights = supeanut_config.MONGO_COLL_EXRIGHTS
		
	def setup(self, with_del = False):
		# init DayData coll
		print '### conn %s ...'%self.coll_daydata
		print self.mongo.conn(self.db_name, self.coll_daydata)
		if with_del is True:
			print '### drop coll %s ...'%self.coll_daydata
			print self.mongo.db.drop_collection(self.coll_daydata)
		print '### create index ...'
		print self.mongo.coll.create_index([('code', 1),('date', 1)],unique = True,name = 'code_date')
		print self.mongo.coll.create_index([('date', 1)], name = 'date')
		# init StockInfo coll
		print '### conn %s ...'%self.coll_stockinfo
		print self.mongo.conn(self.db_name, self.coll_stockinfo)
		if with_del is True:
			print '### drop coll %s ...'%self.coll_stockinfo
			print self.mongo.db.drop_collection(self.coll_stockinfo)
		print '### create index ...'
		print self.mongo.coll.create_index([('code', 1)], name = 'code', unique = True)
		print self.mongo.coll.create_index([('market_prefix', 1)])
		print self.mongo.coll.create_index([('board', 1)])
		print self.mongo.coll.create_index([('stock_name', 1)])
		print self.mongo.coll.create_index([('status', 1)])
		print self.mongo.coll.create_index([('guben', 1)])
		print self.mongo.coll.create_index([('liutong', 1)])
		print self.mongo.coll.create_index([('s_date', 1)])
		print self.mongo.coll.create_index([('e_date', 1)])
		# init Trade Calendar coll
		print '### conn %s ...'%self.coll_trade_calendar
		print self.mongo.conn(self.db_name, self.coll_trade_calendar)
		if with_del is True:
			print '### drop coll %s ...'%self.coll_trade_calendar
			print self.mongo.db.drop_collection(self.coll_trade_calendar)
		print '### create index ...'
		print self.mongo.coll.create_index([('date', 1)], name = 'date', unique = True)
		# init chuquan data coll
		print '### conn %s ...'%self.coll_exrights
		print self.mongo.conn(self.db_name, self.coll_exrights)
		if with_del is True:
			print '### drop coll %s ...'%self.coll_exrights
			print self.mongo.db.drop_collection(self.coll_exrights)
		print '### create index ...'
		print self.mongo.coll.create_index([('code', 1),('date', 1)], name = 'code_date', unique = True)

	def check_info(self, ):
		pass


if __name__ == '__main__':
	obj = MongoSetup()
	obj.setup(with_del = True)
	obj.check_info()
